from P_zong.constant_value import *
from P_zong.get_common_data import get_device_diff_list, get_cim_data_cec, getDeviceId, get_cim_data
from P_zong.get_big_data_P import P_big_data_process
from P_zong.get_zhibiao_P import get_pointCodeForBigdata, P_data_process


def get_elec_P_data(start_time, end_time, station_id, downsample, device_id_list, equipID_list, equipMK_list, device_id,
                    cim_data):
    """
    :param start_time:
    :param end_time:
    :param station_id:
    :param downsample: 采样方式
    :return:
    """
    global P_DATAFRAME_zhibiao_data, P_DATAFRAME_big_data, P_DATAFRAME
    # cim_data = get_cim_data(systemCode=station_id)
    # device_id_list, equipID_list, equipMK_list, device_id = get_device_diff_list(stationId=station_id,
    #                                                                              cim_data=cim_data)
    if equipID_list and not device_id_list:  # 所有进线都是基础测点
        P_DATAFRAME_big_data = P_big_data_process(start_time, end_time, equipID_list, station_id, equipMK_list)
        P_DATAFRAME = P_DATAFRAME_big_data
    elif device_id_list and not equipID_list:  # 所有进线都是指标测点
        d_name, flag_P_exist = get_pointCodeForBigdata(cim_data, device_id, device_id_list)
        P_DATAFRAME_zhibiao_data = P_data_process(device_id_list, station_id, start_time, end_time, d_name, downsample)
        P_DATAFRAME = P_DATAFRAME_zhibiao_data
    elif device_id_list and equipID_list:  # 既有基础测点又有指标测点
        flag_big_data_P_exist = 1
        flag_zhibiao_P_exist = 1
        try:
            P_DATAFRAME_big_data = P_big_data_process(start_time, end_time, equipID_list, station_id, equipMK_list)
        except:
            flag_big_data_P_exist = 0
        try:
            d_name, flag_P_exist = get_pointCodeForBigdata(cim_data, device_id, device_id_list)
            P_DATAFRAME_zhibiao_data = P_data_process(device_id_list, station_id, start_time, end_time, d_name,
                                                      downsample)
        except:
            flag_zhibiao_P_exist = 0
        if flag_zhibiao_P_exist and flag_big_data_P_exist:
            P_DATAFRAME = P_DATAFRAME_big_data + P_DATAFRAME_zhibiao_data
        elif flag_zhibiao_P_exist and not flag_big_data_P_exist:
            P_DATAFRAME = P_DATAFRAME_zhibiao_data
        elif flag_big_data_P_exist and not flag_zhibiao_P_exist:
            P_DATAFRAME = flag_big_data_P_exist
        else:
            raise Exception("缺乏有效数据")
    else:
        raise Exception("未构建设备树")
    return P_DATAFRAME


def get_cec_P_data(start_time, end_time, station_id, downsample):
    """采样方式"""
    cim_data = get_cim_data_cec(systemCode=station_id)
    device_id = getDeviceId(cim_data=cim_data)
    d_name, flag_P_is_exist = get_pointCodeForBigdata(cim_data=cim_data, deviceId_1=device_id, device_id_list=device_id)
    P_DATAFRAME = P_data_process(device_id, station_id, start_time, end_time, d_name, downsample)
    return P_DATAFRAME


def get_cec_and_elec(start_time, end_time, station_id, downsample):
    """
    读取CEC，看是否抛出异常（未构建树，未配置P，数据为空）：
            若无异常，输出CEC树上的总功率
            否则：看ELEC是否存在指标测点
                若不存在：看是否存在基础测点：
                    存在，读取数据
                若存在：看ELEC树指标测点的总进线是否  存在 未配置P的进线（任意一根指标测点进线）
                    若是，退出线程，返回存在进线未配置P
                    否则：
                        读取ELEC树的数据
    """
    try:
        P_DATAFRAME = get_cec_P_data(start_time, end_time, station_id, downsample)
        return P_DATAFRAME
    except:
        cim_data = get_cim_data(systemCode=station_id)
        device_id_list, equipID_list, equipMK_list, device_id = get_device_diff_list(stationId=station_id,
                                                                                     cim_data=cim_data)
        if device_id:  # 所有进线
            if device_id_list:  # 指标测点进线（可能全是指标；或既有指标又有基础）
                d_name, flag_P_is_exist = get_pointCodeForBigdata(cim_data, deviceId_1=device_id,
                                                                  device_id_list=device_id_list)
                if flag_P_is_exist:
                    return "未构建测点P"
                else:
                    P_DATAFRAME = get_elec_P_data(start_time, end_time, station_id, downsample, device_id_list,
                                                  equipID_list, equipMK_list, device_id, cim_data)
            else:  # 全都为基础测点
                P_DATAFRAME = get_elec_P_data(start_time, end_time, station_id, downsample, device_id_list,
                                              equipID_list, equipMK_list, device_id, cim_data)
            return P_DATAFRAME
        else:
            raise Exception("未构建树")


# if __name__ == '__main__':
#     # cim_data = get_cim_data(systemCode=station_id)
#     # device_id_list, equipID_list, equipMK_list, device_id = get_device_diff_list(stationId=station_id,
#     #                                                                              cim_data=cim_data)
#     # P_DATAFRAME = get_elec_P_data(start_time, end_time, station_id, downsample, device_id_list,
#     #                               equipID_list, equipMK_list, device_id, cim_data)
#     # P_DATAFRAME_cec = get_cec_P_data(start_time, end_time, station_id, downsample)
#     P_DATAFRAME_cec_and_elec = get_cec_and_elec(start_time, end_time, station_id, downsample)
